package agent

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchManualRebatesRecordsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 手动返佣--查询
func NewSearchManualRebatesRecordsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchManualRebatesRecordsLogic {
	return &SearchManualRebatesRecordsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SearchManualRebatesRecordsLogic) SearchManualRebatesRecords(req *types.GetManualRebatesReq) (resp *types.GetManualRebatesResp, err error) {
	rpcReq := &rpc.GetManualRebateCommissionRecordsListReq{
		Page:     int64(req.Page),
		PageSize: int64(req.PageSize),
	}

	rpcResp, err := l.svcCtx.AgentRPC.GetManualRebateCommissionRecordsList(l.ctx, rpcReq)
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}

	_data := make([]types.ManualProvideCommissionsRecordsInfo, len(rpcResp.List))
	for _i, _row := range rpcResp.List {
		_data[_i] = types.ManualProvideCommissionsRecordsInfo{
			Id:        _row.ID,
			Date:      _row.Date,
			UserId:    _row.UserId,
			Status:    _row.Status,
			CreatedAt: _row.CreatedAt,
			Remark:    _row.Remark,
		}
	}

	return &types.GetManualRebatesResp{
		List:      _data,
		Page:      int32(rpcResp.Page),
		PageSize:  int32(rpcResp.PageSize),
		TotalPage: rpcResp.TotalPage,
		Total:     rpcResp.Total,
	}, nil

}
